package com.kaikeba.dao;

import com.kaikeba.bean.User;
import com.kaikeba.utils.DBUtil;

import java.sql.*;

public class UserDaoImp implements BaseDao<User> {
    // sql 语句
    private static final String SQL_INSERT = "insert into kkb_user(name,age,city,address,email,phone,weixin,qq,weibo,sex,description) values(?,?,?,?,?,?,?,?,?,?,?)";
    private static final String SQL_FIND_BY_USERID = "select * from kkb_user where id=?"; // *最好不要用，要明确字段

    @Override
    public int insert(User user) {
        // 1.得到链接
        Connection conn = DBUtil.getConn();
        PreparedStatement state = null;
        try {
            // 2.得到执行环境
            state = conn.prepareStatement(SQL_INSERT, Statement.RETURN_GENERATED_KEYS);
            // 3.向执行环境中，填充参数，即填充 sql 语句中的问号
            state.setString(1, user.getName());
            state.setInt(2, user.getAge());
            state.setString(3, user.getCity());
            state.setString(4, user.getAddress());
            state.setString(5, user.getEmail());
            state.setString(6, user.getPhone());
            state.setString(7, user.getWeixin());
            state.setString(8, user.getQq());
            state.setString(9, user.getWeibo());
            state.setString(10, user.getSex());
            state.setString(11, user.getDescription());
            // 4.执行
            state.executeUpdate(); // 执行 sql 语句
            ResultSet rs = state.getGeneratedKeys(); // 获取新增的结果集
            if (rs.next()) {
                // 5.将数据的插入结果返回
                return rs.getInt(1); // 参数 1 表示第一列
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 释放数据库资源
            DBUtil.close(conn, state, null);
        }
        return -1;
    }

    @Override
    public User findByUserId(int userId) {
        try {
            // 1.链接数据库
            Connection conn = DBUtil.getConn();
            // 2.获取执行环境
            PreparedStatement state = conn.prepareStatement(SQL_FIND_BY_USERID);
            // 3.填充执行参数
            state.setInt(1, userId);
            // 4.执行并获取结果
            ResultSet rs = state.executeQuery(); // 执行sql语句
            while (rs.next()) {
                int age = rs.getInt("age");
                String name = rs.getString("name");
                String city = rs.getString("city");
                String address = rs.getString("address");
                String email = rs.getString("email");
                String phone = rs.getString("phone");
                String weixin = rs.getString("weixin");
                String qq = rs.getString("qq");
                String weibo = rs.getString("weibo");
                String sex = rs.getString("sex");
                String description = rs.getString("description");
                User user = new User(name, age, city, address, email, phone, weixin, qq, weibo, sex, description);
                // 5.将结果返回
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }
}
